<?php
/**
 * crpCalendar
 *
 * @copyright (c) 2007, Daniele Conca
 * @link http://code.zikula.org/projects/crpcalendar Support and documentation
 * @author Daniele Conca <conca.daniele@gmail.com>
 * @license GNU/GPL - v.2.1
 * @package crpCalendar
 */

Loader :: includeOnce('modules/crpCalendar/pnclass/crpCalendar.php');

/**
 * initialise block
 *
 */
function crpCalendar_montheventsblock_init()
{
    // Security
    pnSecAddSchema('Montheventsblock::', 'Block title::');
}

/**
 * get information on block
 *
 */
function crpCalendar_montheventsblock_info()
{
  return array('text_type'       => 'crpEvents',
               'module'          => 'crpCalendar',
               'text_type_long'  => 'Month block',
               'allow_multiple'  => true,
               'form_content'    => false,
               'form_refresh'    => false,
               'show_preview'    => true);
}

/**
 * display block
 *
 * @param        array       $blockinfo     a blockinfo structure
 * @return       output      the rendered bock
 */
function crpCalendar_montheventsblock_display($blockinfo)
{
  // security check
  if (!SecurityUtil::checkPermission( 'Montheventsblock::', "$blockinfo[title]::", ACCESS_READ))
    return;

	if(!pnModAvailable('crpCalendar'))
		return;

	$crpcalendar = new crpCalendar();

  // get the current language
  $currentlang = pnUserGetLang();

  // Break out options from our content field
  $vars = pnBlockVarsFromContent($blockinfo['content']);

  $date = $crpcalendar->timeToDMY(time());

	$days = DateUtil :: getMonthDates($date['m'], $date['y']);
	$daysexpanded = $days;
	$monthFirstDay = $days['1'];
	$monthLastDay = $days[count($days)];

	$apiargs['startDate'] = DateUtil :: getDatetime($crpcalendar->backToFirstDOW(DateUtil :: parseUIDateTime($monthFirstDay)));
	$apiargs['endDate'] = DateUtil :: getDatetime($crpcalendar->forwardToLastDOW(DateUtil :: parseUIDateTime($monthLastDay)));
	$apiargs['sortOrder'] = 'ASC';
	$apiargs['active'] = 'A';
	// reset page limit for daylist
	$apiargs['modvars']['itemsperpage'] = '-1';

  // call the api
  $items = pnModAPIFunc('crpCalendar', 'user', 'getall', $apiargs);

  // expand days array
	$crpcalendar->expandFirstDOW(DateUtil :: parseUIDateTime($monthFirstDay), $daysexpanded);
	$crpcalendar->expandLastDOW(DateUtil :: parseUIDateTime($monthLastDay), $daysexpanded);

  // create the output object
  $pnRender = pnRender::getInstance('crpCalendar',false);

	$pnRender->assign('displayweek', $vars['displayweek']);
	$pnRender->assign('displayevents', $vars['displayevents']);
  $pnRender->assign('events', $items);
  $pnRender->assign('days', $days);
  $pnRender->assign('daysexpanded', $daysexpanded);
  $pnRender->assign('date', $date);
  $pnRender->assign('t', time());
  $pnRender->assign('todayEv', DateUtil :: getDatetime(time()));
  $pnRender->assign(pnModGetVar('crpCalendar'));
  $pnRender->assign('day_of_week_short', $crpcalendar->ui->day_of_week_short);

  $blockinfo['content'] = $pnRender->fetch('blocks/crpcalendar_block_month.htm');
  return pnBlockThemeBlock($blockinfo);
}

/**
 * modify block settings
 *
 * @param        array       $blockinfo     a blockinfo structure
 * @return       output      the bock form
 */
function crpCalendar_montheventsblock_modify($blockinfo)
{
  // Break out options from our content field
  $vars = pnBlockVarsFromContent($blockinfo['content']);

  // Defaults
  if (empty($vars['displayweek']))
    $vars['displayweek'] = null;

  if (!isset($vars['displayevents']))
      $vars['displayevents'] = null;

  // Create output object
  $pnRender = pnRender::getInstance('crpCalendar', false);

  // assign the block vars
  $pnRender->assign($vars);

  // Return the output that has been generated by this function
  return $pnRender->fetch('blocks/crpcalendar_block_month_modify.htm');
}

/**
 * update block settings
 *
 * @param        array       $blockinfo     a blockinfo structure
 * @return       $blockinfo  the modified blockinfo structure
 */
function crpCalendar_montheventsblock_update($blockinfo)
{
  // Get current content
  $vars = pnBlockVarsFromContent($blockinfo['content']);

  // alter the corresponding variable
  $vars['displayweek']       = (bool)FormUtil::getPassedValue('displayweek', null, 'POST');
  $vars['displayevents']    = (bool)FormUtil::getPassedValue('displayevents', null, 'POST');

  // write back the new contents
  $blockinfo['content'] = pnBlockVarsToContent($vars);

  // clear the block cache
  $pnRender = pnRender::getInstance('crpCalendar');
  $pnRender->clear_cache('blocks/crpcalendar_block_month.htm');

  return $blockinfo;
}

?>